< previous page page_243 next page >

Page 243
e10a6a0a59f035e870d90cca05b85fce.gif
                    BallRadius(ball)) - BallX(ball)
                BallDx(ball) = -BallDx(ball)
            End If

            BallY(ball) = BallY(ball) + BallDy(ball)
            If BallY(ball) < BallRadius(ball) Then
                BallY(ball) = 2 * BallRadius(ball) - _
                    BallY(ball)
                BallDy(ball) = -BallDy(ball)
            ElseIf BallY(ball) > ymax - BallRadius(ball) Then
                BallY(ball) = 2 * (ymax - _
                    BallRadius(ball)) - BallY(ball)
                BallDy(ball) = -BallDy(ball)
            End If
        Next ball

        If Not Playing Then Exit Do
    Loop
End Sub
This technique works well when the animation is moving a few small objects across a large background. In that case, the program needs to save and restore just a few small areas. The clear and redraw technique, on the other hand, would need to clear a large background area.
Using the API
Programs Stocks2, Bounce1, and Bounce2 all have marginal performance. For a reasonably small drawing area on a 133MHz Pentium, program Bounce2 can display around 12 frames per second. Program Stocks2 can display only around 8 frames per second. This is just barely fast enough to create meaningful animation.
You can improve the speed of these programs significantly using API functions. Programs Stocks2 and Bounce2 use PaintPicture to copy parts of pictures. You can make them faster if you use the BitBlt API function instead.
Program Bounce1 uses the Cls method to restore its background before drawing a new frame in the animation. To make this program faster, save the background's bitmap information in a byte array using the GetBitmapBits API function. Then instead of using Cls to restore the background, use SetBitmapBits. Programs Stocks2b, Bounce1b, and Bounce2b use these methods to improve performance.
Table 8.2 summarizes the speeds of these techniques. Programs Bounce1b and Bounce2b that use the API functions are always faster than programs Bounce1 and Bounce2, but neither is always fastest. Bounce1b is faster when the program is moving many objects. Bounce2b is faster when the program is moving only a few objects.

 
< previous page page_243 next page >